package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.community.entity.Admin;
import com.wanmait.community.entity.AdminRole;
import com.wanmait.community.mapper.AdminMapper;
import com.wanmait.community.mapper.AdminRoleMapper;
import com.wanmait.community.service.AdminService;
import com.wanmait.community.util.AjaxResult;
import com.wanmait.community.util.JWTUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService{
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Value("${token.expireMinutes}")
    private int expireMinutes;
    @Value("${token.secret}")
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Admin> findAll() {
        return adminMapper.selectList(null);
    }

    @Override
    public AjaxResult login(Admin admin) {
        //首先检查用户名和密码是否合法
        if(admin == null || StringUtils.isEmpty(admin.getAdminName())||StringUtils.isEmpty(admin.getAdminPass())){
            return AjaxResult.error(10001,"用户名或者密码为空");
        }
        System.out.println(admin);
        //根据用户名密码查询用户
        Admin loginAdmin = adminMapper.selectBynamePassword(admin.getAdminName(),admin.getAdminPass());
        if(loginAdmin==null){
            return AjaxResult.error(10001,"用户名或者密码错误");
        }
        if(loginAdmin==null){
            return null;
        }
        //跟新登陆信息
        Admin updateAdmin = new Admin();
        updateAdmin.setId(loginAdmin.getId());
        updateAdmin.setLoginIp(loginAdmin.getLoginIp());
        updateAdmin.setLoginTime(new Date());
        adminMapper.updateById(updateAdmin);

        //生成token
        Map<String,String> claims = new HashMap<>();
        claims.put("adminName",loginAdmin.getAdminName());
        String token = JWTUtils.createToken(loginAdmin.getId().toString(),expireMinutes,admin.getAdminPass(),claims);
        //把token放到Redis中
        redisTemplate.opsForValue().set("token:"+token,loginAdmin,expireMinutes, TimeUnit.MINUTES);
        loginAdmin.setAdminPass(null);

        return AjaxResult.success("登陆成功")
                        .put("token",token)
                        .put("admin",loginAdmin)
                        .put("identity",1);
    }




    @Override
    public Admin findById(Integer id) {
        return adminMapper.findByid(id);
    }

    @Override
    public Admin selectAdminByPhone(String phone) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getPhone,phone);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Admin selectAdminByInNum(String inNum) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getInNum,inNum);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public void insertAdmin(Admin admin,Integer roleId) {
        adminMapper.insertAdmin(admin);
        adminRoleMapper.insertAdminRole(new AdminRole().setAdminId(admin.getId()).setRoleId(roleId));
    }

    @Override
    public void deleteAdminById(Integer id) {
        adminMapper.deleteById(id);
        adminRoleMapper.deleteAdminRoleById(id);
    }
}
